package com.agricultural.machine.chain;

import com.agricultural.machine.common.IMachine;
import com.agricultural.machine.factory.MachineFactory;
import com.agricultural.machine.state.MaintenanceState;

/**
 * 故障处理器类 - 责任链模式
 */
public class FaultHandler extends AbstractSafetyHandler {
    private final MachineFactory machineFactory;
    
    public FaultHandler() {
        this.machineFactory = MachineFactory.getInstance();
    }
    
    @Override
    public boolean handle(SafetyEvent event) {
        if (event.getEventType().equals("FAULT")) {
            System.out.println("故障处理器处理事件: " + event.getDescription());
            
            // 获取农机
            IMachine machine = machineFactory.getMachine(event.getMachineId());
            if (machine == null) {
                System.out.println("无法找到农机: " + event.getMachineId());
                return false;
            }
            
            // 根据故障类型处理
            String faultType = event.getEventData();
            switch (faultType) {
                case "MINOR":
                    handleMinorFault(machine);
                    break;
                case "MODERATE":
                    handleModerateFault(machine);
                    break;
                case "SEVERE":
                    handleSevereFault(machine);
                    break;
                default:
                    System.out.println("未知故障类型: " + faultType);
                    return handleNext(event);
            }
            
            // 标记事件已处理
            event.setHandled(true);
            return true;
        }
        
        // 不是故障事件，传递给下一个处理器
        return handleNext(event);
    }
    
    /**
     * 处理轻微故障
     */
    private void handleMinorFault(IMachine machine) {
        System.out.println("处理轻微故障: 尝试自动修复");
        
        // 停止农机
        machine.stop();
        
        // 模拟自动修复过程
        try {
            System.out.println("自动诊断中...");
            Thread.sleep(1000);
            System.out.println("应用修复操作...");
            Thread.sleep(1000);
            System.out.println("测试系统...");
            Thread.sleep(1000);
            System.out.println("轻微故障已修复，农机可以继续工作");
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
    
    /**
     * 处理中等故障
     */
    private void handleModerateFault(IMachine machine) {
        System.out.println("处理中等故障: 需要临时停机");
        
        // 停止农机
        machine.stop();
        
        // 模拟修复过程
        try {
            System.out.println("进行深度诊断...");
            Thread.sleep(1500);
            System.out.println("重置系统组件...");
            Thread.sleep(1500);
            System.out.println("校准传感器...");
            Thread.sleep(1500);
            System.out.println("中等故障已修复，农机可以继续工作");
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
    
    /**
     * 处理严重故障
     */
    private void handleSevereFault(IMachine machine) {
        System.out.println("处理严重故障: 需要进入维护状态");
        
        // 停止农机并设置为维护状态
        machine.stop();
        machine.setState(new MaintenanceState());
        
        System.out.println("严重故障需要人工干预，农机已进入维护状态");
        System.out.println("请联系技术人员进行维修");
    }
} 